package clei.sample

import android.annotation.SuppressLint
import android.util.Log
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

/**
 *  create by chenglei at 2018/10/25
 */


object LanguageRepository {


    fun requestLanguagesSync(): List<Language> {
        return LanguageApi.getLanguageRetrofit()
                .getLaungageSync()
                .execute()
                .body()?.data ?: emptyList()
    }


    @SuppressLint("CheckResult")
    fun requestLanguages(success:(MyResponse)->Unit, error: (Throwable)->Unit) {
        LanguageApi.getLanguageRetrofit()
                .getLanguage()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe { t ->
                    if (t == null) {
                        Log.e(javaClass.simpleName, "isnull")
                    } else {
                        success(t)
                    }
                }
//                .subscribe(object : Subscriber<MyResponse> {
//                    override fun onComplete() {
//                        Log.e(javaClass.simpleName, "onComplete")
//
//                    }
//
//                    override fun onSubscribe(s: Subscription?) {
//                        Log.e(javaClass.simpleName, "onSubscribe")
//                    }
//
//                    override fun onNext(t: MyResponse?) {
//                        Log.e(javaClass.simpleName, "onNext")
//
//                        t?.let {
//                            success(it)
//                        }
//                    }
//
//                    override fun onError(t: Throwable?) {
//                        Log.e(javaClass.simpleName, "onError")
//                        t?.let {
//                            error(it)
//                        }
//                    }


//                })
    }

}